<div class="layui-fluid" lay-title="表单组合">
    <div class="layui-card">
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form" action="" lay-filter="form-group">
                <div class="layui-form-item">
                    <label class="layui-form-label">字典类型</label>
                    <div class="layui-input-block">
                        <input type="text" name="dictType" disabled lay-verify="required" autocomplete="off" placeholder="请输入字典类型" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="required" autocomplete="off" placeholder="请输入名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">键值</label>
                    <div class="layui-input-block">
                        <input type="text" name="value" lay-verify="required" autocomplete="off" placeholder="请输入键值" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">显示顺序</label>
                    <div class="layui-input-block">
                        <input type="text" name="orderNum" lay-verify="required|number" autocomplete="off" placeholder="请输入显示顺序" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="有效" checked="">
                        <input type="radio" name="status" value="0" title="无效">
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit="" lay-filter="form-group-submit">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
    layui.use(['jquery', 'admin', 'form','layer'], function () {
        var $ = layui.jquery
            , layer = layui.layer
            , form = layui.form
            , admin = layui.admin;
        var sysDictData = {};
        var id = POPUP_DATA.id;
        var type = POPUP_DATA.type;
        var table = POPUP_DATA.table;
        form.render(null, 'form-group');
        form.val("form-group", {
            "dictType": type
        });
        // 初始化
        admin.get({
            type: 'get',
            url: '/sysDictData/info/' + id + '&token=' + admin.loginToken,
            dataType: 'json',
            success: function (data) {
                sysDictData = data.sysDictData;
                if (sysDictData) {
                    form.val("form-group", {
                        "name": sysDictData.name // "name": "value"
                        , "value": sysDictData.value
                        , "orderNum": sysDictData.orderNum
                        , "status": sysDictData.status
                    });
                }
            }
        });

        form.on('submit(form-group-submit)', function (data) {
            var field = data.field;
            var dictData = {};
            var url = "/sysDictData/save";
            if (id) {
                url = "/sysDictData/update?_method=PUT";
                dictData.id = id;
            }
            dictData.name = field.name;
            dictData.dictType = field.dictType;
            dictData.status = field.status;
            dictData.value = field.value;
            dictData.orderNum = field.orderNum;

            admin.post({
                url: url,
                contentType: "application/json",
                data: JSON.stringify(dictData),
                success: function (res) {
                    if (res.code == 0) {
                        // 提醒保存成功，关闭当前弹窗
                        layer.msg(res.msg);
                        layer.closeAll();
                        //刷新
                        table.reload('list-table-dictData', {
                            url: '/sysDictData/page?token=' + admin.loginToken,
                            page: {
                                curr: 1
                            }
                        })
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });
</script>